package com.eastseeway.radioproject.setting;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.eastseeway.radioproject.base.BaseViewModel;
import com.eastseeway.radioproject.base.Const;
import com.eastseeway.radioproject.entities.OperationParams;
import com.eastseeway.radioproject.entities.UserInfo;
import com.google.gson.Gson;

import java.util.List;

public class SettingViewModel extends BaseViewModel<SettingRepository> {


    private MutableLiveData<String> toastInfo = new MutableLiveData<>();
//    private MutableLiveData<String> userName = new MutableLiveData<>("");

    private LiveData<List<UserInfo>> allUsers = new MutableLiveData<>();

    public SettingViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    protected SettingRepository setRepository() {
        return new SettingRepository( getApplication());
    }

    public void addUser(String username,String password){
//        if (userName.getValue().isEmpty()) {
//            Toast.makeText(getApplication(),"请输入用户名",Toast.LENGTH_SHORT).show();
//            return;
//        }
//        if (password.getValue().isEmpty() || password.getValue().length() < 6) {
//            Toast.makeText(getApplication(),"请输入正确的密码",Toast.LENGTH_SHORT).show();
//            return;
//        }
        if (isUserExist(username)) {
            toastInfo.setValue("用户已经存在");
        }else {
            UserInfo userInfo = new UserInfo();
            userInfo.setName(username);
            userInfo.setPassword(password);
            userInfo.setIs_admin(false);
            Log.e("12121","addUser "+new Gson().toJson(userInfo));
            mRepository.addUser(userInfo);
//            addRadioParams(username);
        }
    }


//    void addRadioParams(String username){
//        for (int i = 0; i < Const.POSITION_PREVIOUS_PARAMS; i++) {
//            OperationParams operationParams = new OperationParams();
//            operationParams.setPosition(i+1);
//            operationParams.setUsername(username);
//            operationParams.setEmpty(true);
//            mRepository.addRadioParams(operationParams);
//        }
//    }

    void updateUser(UserInfo userInfo){
        mRepository.updateUser(userInfo);
    }

    void deleteUser(UserInfo userInfo){
        mRepository.deleteUser(userInfo);
    }

    void deleteAll(){
        mRepository.deleteAll();
    }

    public void queryAllUsers() {
        allUsers = mRepository.queryAll();
    }

    public boolean isUserExist(String username){
        boolean isExist = false;
        for (UserInfo userInfo : getAllUsers().getValue()) {
            if (userInfo.getName().equals( username)) {
                isExist = true;
                break;
            }
        }
        return isExist;
    }

    public LiveData<List<UserInfo>> getAllUsers() {
        return allUsers;
    }

    public MutableLiveData<String> getToastInfo() {
        return toastInfo;
    }
}
